//
//  GYZAllCountryViewController.m
//  Qyer
//
//  Created by GYz on 15/10/21.
//  Copyright © 2015年 GF. All rights reserved.
//

#import "GYZAllCountryViewController.h"
#import "BaseHeader.h"
@interface GYZAllCountryViewController () <UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, assign)int page;

@end

@implementation GYZAllCountryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.infoArr = [[NSMutableArray alloc]init];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBarHidden = YES;
    
    _page = 1;
    
    [self creatNav];
    [self afn:1];
}

#pragma mark - 创建navigation
- (void)creatNav
{
    UIView *nav = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    nav.userInteractionEnabled = YES;
    nav.backgroundColor = [UIColor colorWithRed:250./255 green:250./255 blue:250./255 alpha:.1];
    
    UILabel *title = [self creatLabelWithRect:CGRectMake(0, 20, SCREEN_WIDTH, 30) text:@"全部城市" font:nil textColor:nil alignment:YES];
    [nav addSubview:title];
    
    UIButton *backBut = [self creatButtonWithRect:CGRectMake(10, 20, 30, 30) imageStr:@"iconfont-31fanhui1" click:@selector(gobackView)];
    [nav addSubview:backBut];
    
    [self.view addSubview:nav];
}

- (void)gobackView
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 创建collectionView
- (void)creatCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH * 3.1 / 7, SCREEN_WIDTH * 4 / 7);
    flowLayout.sectionInset = UIEdgeInsetsMake(SCREEN_WIDTH/35, SCREEN_WIDTH/25, SCREEN_WIDTH/35, SCREEN_WIDTH/25);
    
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64) collectionViewLayout:flowLayout];
    _collectionView.backgroundColor = [UIColor colorWithRed:240./255 green:240./255 blue:240./255 alpha:1];
    [_collectionView setDelegate:self];
    [_collectionView setDataSource:self];
    [self.view addSubview:_collectionView];
    [_collectionView registerClass:[GYZAllCountryCollectionViewCell class] forCellWithReuseIdentifier:@"AllCountry"];
    [self allData];
}

#pragma mark - collectionView代理方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.infoArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    GYZAllCountryCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AllCountry" forIndexPath:indexPath];
    GYZAllCountryModel *model = self.infoArr[indexPath.row];
    cell.backgroundColor = [UIColor redColor];
    [cell setValueWithModel:model];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    GYZCityViewController *city = [[GYZCityViewController alloc] init];
    GYZAllCountryModel *model = self.infoArr[indexPath.row];
    city.cid = model.cid;
    city.cnname = model.catename;
    city.enname = model.catename_en;
    [self.navigationController pushViewController:city animated:YES];
}

#pragma mark - 网络请求&数据解析
- (void)afn:(int)number
{
    [AFN getdataWithString:[NSString stringWithFormat:@"http://open.qyer.com/place/city/get_city_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&countryid=%@&oauth_token=d5ecb9febb61510d54ba2d47d986cf65&page=%d&track_app_version=6.6.6&track_user_id=6695170&v=1", self.urlStr, number] block:^(id block) {
        
        for (NSDictionary *dataDic in [block objectForKey:@"data"]) {
            GYZAllCountryModel *model = [GYZAllCountryModel modelWithDictionary:dataDic];
            [self.infoArr addObject:model];
        }
        if (!_collectionView) {
            [self creatCollectionView];
        }
        [_collectionView reloadData];
        [_collectionView.footer endRefreshing];
        
    } BLOCK:^(id BLOCK) {
    }];
}

-(void)allData
{
    __block GYZAllCountryViewController *allCountry = self;
    [_collectionView addLegendFooterWithRefreshingBlock:^{
        allCountry.page += 1;
        [allCountry afn:allCountry.page];
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
